/**
 * 
 */
package com.gr.staffpm.widget.dialog.task;

import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.odlabs.wiquery.core.javascript.JsScope;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.core.options.ListItemOptions;
import org.odlabs.wiquery.ui.core.JsScopeUiEvent;
import org.odlabs.wiquery.ui.dialog.Dialog;
import org.odlabs.wiquery.ui.dialog.DialogButton;

import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.model.UserChoiceRenderer;

/**
 * @author Graham Rhodes 31 Dec 2010 16:45:14
 */
public class AjaxAssignDialog extends Dialog {

    private static final long serialVersionUID = 1L;
    @SpringBean(name = "userService")
    private UserService userService;
    private User currentAssignee;
    private final AbstractDefaultAjaxBehavior formAjaxBehavior;
    private final Form<String> assignForm = new Form<String>("assignForm");
    private final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(assignForm));

    public AjaxAssignDialog(String id, User currentAssignee) {
        super(id);
        this.setCurrentAssignee(currentAssignee);

        feedbackPanel.setOutputMarkupPlaceholderTag(true);
        add(feedbackPanel);

        formAjaxBehavior = new AbstractDefaultAjaxBehavior() {

            private static final long serialVersionUID = 1L;

            /* (non-Javadoc)
             * @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget)
             */
            @Override
            protected void respond(AjaxRequestTarget target) {
                // Here, we retrieve data form the form of the advanced dialog
                assignForm.onFormSubmitted();

                if (assignForm.hasError()) {
                    // Error
                    target.addComponent(feedbackPanel);
                } else {
                    getSession().cleanupFeedbackMessages();
                    // TODO Here update task
                    close(target);
                }
            }
        };
        add(formAjaxBehavior);

        assignForm.setOutputMarkupId(true);
        assignForm.setMarkupId("assignForm");

        DropDownChoice<User> assignee = new DropDownChoice<User>("assignee", new PropertyModel<User>(this, "currentAssignee"), userService.getAllUsers(), new UserChoiceRenderer());
        assignForm.add(assignee);

        add(assignForm);

        ListItemOptions<DialogButton> assignButtons = new ListItemOptions<DialogButton>();
        assignButtons.add(new DialogButton("Cancel", JsScope.quickScope(close().render())));
        //assignButtons.add(new DialogButton("Save", JsScope.quickScope("wicketSubmitFormById('assignForm','"
        //+ formAjaxBehavior.getCallbackUrl()
        //+ "', null, null, null, null, null);")));

        setButtons(assignButtons);
        setTitle("Assign task to user");
        setCloseOnEscape(false);
        setModal(true);

        setOpenEvent(JsScopeUiEvent.quickScope(new JsStatement().self().chain("parents", "'.ui-dialog:first'").chain("find", "'.ui-dialog-titlebar-close'").chain("hide").render())); // When we open the dialog, we remove the close button on the title

    }

    public void setCurrentAssignee(User currentAssignee) {
        this.currentAssignee = currentAssignee;
    }

    public User getCurrentAssignee() {
        return currentAssignee;
    }

}
